package com.yzkj.siot.facade.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yzkj.siot.context.CurUserCtx;
import com.yzkj.siot.domain.TenantProductData;
import com.yzkj.siot.exp.BizExp;
import com.yzkj.siot.facade.ProductDataFacade;
import com.yzkj.siot.result.PageParams;
import com.yzkj.siot.result.PageResponse;
import com.yzkj.siot.service.TenantProductDataService;
import com.yzkj.siot.service.TenantProductService;
import com.yzkj.siot.util.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class ProductDataFacadeImpl implements ProductDataFacade {

    @Autowired
    TenantProductDataService productDataService;

    @Autowired
    TenantProductService productService;

    @Autowired
    PageHelper<TenantProductData> productDataPageHelper;

    @Override
    public PageResponse<TenantProductData> page(PageParams<TenantProductData> pageParams) {
        return productDataPageHelper.queryPageOrList(productDataService, pageParams, null);
    }

    @Override
    public TenantProductData queryOne(int id) {

        return productDataService.getById(id);
    }

    @Override
    public TenantProductData saveOrUpdate(TenantProductData entity) {

        // 若是需要更新，校验是否存在此id的数据解析协议
        if (entity.getId() != null && entity.getId() != 0 && productDataService.getById(entity.getId()) == null) {
            throw new BizExp("此协议不存在或已被删除！");
        }

        // 校验产品
        if (entity.getProductId() == null || productService.getById(entity.getProductId()) == null) {
            throw new BizExp("不得绑定不存在或已被删除的产品！");
        }

        // 校验重名
        QueryWrapper<TenantProductData> productDataQueryWrapper = new QueryWrapper<>();
        productDataQueryWrapper.lambda()
                .eq(TenantProductData::getDataName, entity.getDataName())
                .eq(TenantProductData::getProductId, entity.getProductId())
                .ne(TenantProductData::getId, entity.getId());

        if (productDataService.count(productDataQueryWrapper) != 0) {
            throw new BizExp("已存在同名数据解析项！");
        }

        if (entity.getId() == null || entity.getId() == 0) {
            entity.setTenantId(CurUserCtx.get().getTenantId());
        }

        if (!productDataService.saveOrUpdate(entity)) {
            throw new BizExp("保存数据解析项时出现未知错误！");
        }

        log.info("保存数据解析协议 => {}", entity);

        return queryOne(entity.getId());
    }

    @Override
    public TenantProductData remove(int id) {
        TenantProductData productData = queryOne(id);

        if (productData == null) {
            throw new BizExp("此数据解析项不存在或已被删除！");
        }

        if (!productDataService.removeById(id)) {
            throw new BizExp("删除数据解析项时发生错误！");
        }

        log.info("删除数据解析项 => {}", productData);
        return productData;
    }

    @Override
    public Boolean batchRemove(List<Integer> idList) {
        return null;
    }

    @Override
    public Boolean batchSave(List<TenantProductData> entities) {
        return null;
    }
}
